<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addPermission(0,1)">添加权限</el-button>
      <el-table default-expand-all :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column v-slot="{row}" label="操作">
          <el-button v-if="(row.type === 1)" size="mini" type="text" @click="addPermission(row.id,2)">添加</el-button>
          <el-button size="mini" type="text" @click="editPermission(row.id)">编辑</el-button>
          <el-button size="mini" type="text" @click="delPermission(row.id)">删除</el-button>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :title="`${showText}权限点`"
      :visible="showDialog"
      width="30%"
      :before-close="cancel"
    >
      <!-- 表单 -->
      <el-form ref="perFormRef" :model="formData" :rules="rulesForm" label-width="120px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>

        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>

        <el-form-item label="权限描述">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>

        <el-form-item label="开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirmForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addPermissionApi, delPermissionApi, getPermissionDetailApi, getPermissionListApi, updatePermissionApi } from '@/api/permission'
import { transObjToTree } from '@/utils'

export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 默认关闭
      },
      rulesForm: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.list = transObjToTree(await getPermissionListApi(), 0)
    },
    addPermission(pid, type) {
      // pid:  页面权限点pid值为'0', 按钮权限点值为所属页面权限点的id值
      // type: 1为页面路由权限点, 2为按钮权限点
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
    },
    async confirmForm() {
      this.$refs.perFormRef.validate()
      if (this.formData.id) {
        await updatePermissionApi(this.formData)
      } else {
        await addPermissionApi(this.formData)
      }
      this.getPermissionList()
      this.showDialog = false
      this.$message.success(this.formData.id ? `编辑成功` : `添加成功`)
      this.cancel()
    },
    cancel() {
      this.$refs.perFormRef.resetFields()
      this.formData.description = ''
      this.formData.enVisible = 0
      this.showDialog = false
    },
    async editPermission(id) {
      this.formData = await getPermissionDetailApi(id)
      this.getPermissionList()
      this.showDialog = true
    },
    async delPermission(id) {
      await this.$confirm('确定要删除该数据吗')
      await delPermissionApi(id)
      this.getPermissionList()
      this.$message.success('删除成功')
    }
  }
}
</script>

<style>
.btn-add {
  margin: 10px;
}
</style>

